﻿using AdminApi.Extension.Permission;
using AdminApi.Extension.Permission.Attributes;
using AdminApi.Extension.Permission.Model;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace AdminApi.Extension
{
    public class OperationLogFilter : IActionFilter, IOrderedFilter
    {
        public int Order => 1;


        private PermissionMenuInfo perssionMenuInfo;
        private IDictionary<string, object> payLoad;
        private IDictionary<string, object> actionArguments;

        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.HttpContext.Items.ContainsKey("payLoad")) return;

            payLoad = (Dictionary<string, object>)context.HttpContext.Items["payLoad"];
            if (payLoad == null || payLoad.Count == 0) return;

            ControllerActionDescriptor controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            var mtoken = controllerActionDescriptor.MethodInfo.MetadataToken;

            perssionMenuInfo = PressionManager.GetPerssionMenuInfoByMetadataToken(mtoken);
            if (perssionMenuInfo == null) return;

            if (perssionMenuInfo.MenuType != MenuType.菜单列表
                || perssionMenuInfo.MenuType != MenuType.分组菜单
                || perssionMenuInfo.MenuType != MenuType.查询)
            {
                var actionName = perssionMenuInfo.ParentPath;
                var fullpath = perssionMenuInfo.MethodInfo.Name;

                #region 添加入库记录

                var data = context.ActionDescriptor.Parameters;
                var json = string.Empty;

                foreach (var item in data)
                {
                    var parameterName = item.Name;
                    var parameterType = item.ParameterType;

                    if (!parameterName.Contains("Business") && !parameterName.Contains("Api")&& context.ActionArguments.ContainsKey(parameterName))
                    {
                        var a = context.ActionArguments[parameterName];

                        json += (item.Name + "：" + JsonConvert.SerializeObject(a));
                    }
                }

                //AdminBusiness business = ServiceProvider.GetService<AdminBusiness>();
                //business.InsertSystemLog(payLoad["id"].ToString(), actionName, fullpath, json);

                #endregion


                payLoad = null;
                perssionMenuInfo = null;
                return;
            }
            actionArguments = context.ActionArguments;




        }
        public void OnActionExecuted(ActionExecutedContext context)
        {
            /*
                payLoad.Add("id", admin.ID);
                payLoad.Add("jti", Guid.NewGuid().ToString());
                payLoad.Add("name", admin.AdminName);
                payLoad.Add("realname", admin.RealName);
                payLoad.Add("phone", admin.AdminPhone);
                payLoad.Add("companyid", admin.CompanyId);
                payLoad.Add("time", DateTime.Now);
                payLoad.Add("expires", 7200);
                payLoad.Add("pid", adminRole?.ID);
             */
            if (payLoad == null || payLoad.Count == 0) return;
            if (perssionMenuInfo == null) return;

            var UserID = (int)(long)payLoad["id"];
            var UserName = (string)payLoad["name"];
            var RealName = (string)payLoad["realname"];
            var Phone = (string)payLoad["phone"];
            var CompanyID = (int)(long)payLoad["companyid"];

            var Path = context.HttpContext.Request.Path;
            var Method = context.HttpContext.Request.Method;
            //actionArguments
            var Result = context.Result;
        }
    }
}
